define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activtyc/round/index',
                    add_url: 'activtyc/round/add',
                    edit_url: 'activtyc/round/edit',
                    del_url: 'activtyc/round/del',
                    multi_url: 'activtyc/round/multi',
                    byOut_url: 'activtyc/round/by_out',
                    table: 'activity_round',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                         searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                         {field: 'url', title: __('Url'), formatter: Controller.api.formatter.avatar},
                          {field: 'volume_text', title: __('Volume_id')},
                        {field: 'name', title: __('Name')},
                        {field: 'out_trade_no', title: __('商户订单号')},
                        {field: 'phone', title: __('Phone')},
                       {field: 'shen', title: __('Shen'),formatter: Controller.api.formatter.province},
                        {field: 'shi', title: __('Shi'),formatter: Controller.api.formatter.shi},
                        {field: 'xian', title: __('Xian'),formatter: Controller.api.formatter.xian},
                        {field: 'address', title: __('Address')},
                        {field: 'type_school', title: __('Type_school'), searchList: {"1":__('Type_school 1'),"2":__('Type_school 2')},formatter: Table.api.formatter.normal},
                      
                        {field: 'school_text', title: __('School_id')},
                        {field: 'book_text', title: __('Book_id')},
                        // {field: 'type', title: __('版本')},
                        {field: 'classname', title: __('班级')},
                          {field: 'pays', title: __('表单类型'), searchList: {"1":__('送学校'),"2":__('线下取'),"3":__('9元邮费'),"4":__('支付成功'),"5":__('退款')},formatter: Table.api.formatter.normal},
                         {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                avatar: function (value, row, index) {
                  return '<img width="45" height="45" src="'+value+'" class="img-circle">';
                },
                province: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                    }
                  return areatext;
                },
                shi: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                       
                    }
                  return areatext;
                },
                xian: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                       
                    }
                  return areatext;
                },
                operate: function (value, row, index) {
                    var table = this.table;
                    // 操作配置
                    var options = table ? table.bootstrapTable('getOptions') : {};
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    if (options.extend.byOut_url !== ''){
                        buttons.push({
                            name: 'books',
                            icon: 'fa fa-paper-plane',
                            title: __('退款'),
                            text:'退款',
                            extend: 'data-toggle="tooltip"',
                            area:['50%', '80%'],
                            classname: 'btn btn-xs btn-success btn-out',
                            url: options.extend.byOut_url,
                            hidden:function (row) {
                                if(row.pays == 4 || row.pays == 3)
                                {
                                    return false;
                                }
                                return true;


                            }
                        });
                    }
                    if (options.extend.edit_url !== ''){
                        buttons.push({
                            name: 'edit',
                            icon: 'fa fa-pencil',
                            title: __('Edit'),
                            extend: 'data-toggle="tooltip"',
                            classname: 'btn btn-xs btn-success btn-editone',
                            url: options.extend.edit_url
                        });
                    }
                    if (options.extend.del_url !== '') {
                        buttons.push({
                            name: 'del',
                            icon: 'fa fa-trash',
                            title: __('Del'),
                            extend: 'data-toggle="tooltip"',
                            classname: 'btn btn-xs btn-danger btn-delone',
                            url: options.extend.del_url
                        });
                    }
                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                }
            }
        }
    };
    return Controller;
});